********************************** 
*** Replication Package - Main ***
**********************************

version 18
clear all
set scheme burd
run "analysis_programs.do"
	
******************************************************* 
*** Table 1. Vignette experiment: Summary statistics **
*******************************************************

use "vignette_experiment.dta" , clear
global sample idtag & !everinattentive

global save "Table1.doc"
global analysisvar isfemale age isparttime taxibefore ///
  hourspermth ridesperhour currentcomm incomelesscomm ///
  earnings selfseg test1correctovernondks ///
  amenity_code
  
preserve
winsor2 ridesperhour incomelesscomm earnings, cuts(5,95) replace
asdoc summ $analysisvar if $sample , ///
  label dec(2) font(TimesRoman) fs(12) ///
  title(\b0 Summary statistics) replace
restore

*********************************************
*** Figure 1. Vignette experiment: Skills ***
*********************************************

use "vignette_experiment.dta" , clear
global sample idtag & !everinattentive

preserve
keep if $sample  
collapse (mean) mean = test1seg (sd) sd = test1seg ///
  (count) n = test1seg , by(selfseghigh)

generate uc = mean + invttail(n-1,0.025)*(sd / sqrt(n))
generate lc = mean - invttail(n-1,0.025)*(sd / sqrt(n))
format mean %4.2f
format lc uc  %4.1f

global graphopt barw(0.5) lw(medium) mlw(medthick) color("33 102 172")
twoway ( bar mean selfseghigh if selfseghigh == 0 , ///
    $graphopt bargap(-50) ) ///
  ( bar mean selfseghigh if selfseghigh == 1 , ///
    $graphopt bargap(50) ) ///
  ( rcap lc uc selfseghigh , lwidth(thick) lcolor(gs5) ///
     /* color("178 24 43") */ ) ///
  ( scatter mean selfseghigh , msymbol(i) ///
    mlabel(mean) mlabpos(1) mlabsize(large) mlabcol(gs0) ) , ///
  xtitle( "Driving knowledge" , size(large) ) ///
  xlabel(0 "Low" 1 "High" , noticks labsize(large) ) /// 
  ytitle( "Location test (1 = lowest; 7 = highest)" , ///
    size(large) ) ///
  ylabel( , labsize(large) format(%3.1f) ) legend( off )
graph export "Figure1.png"    
restore

***************************************************************************
*** Table 2. Vignette experiment: Effect on map app technology on choice ** 
*************************************************************************** 

use "vignette_experiment.dta" , clear

global sample inrange(pair,11,19) & !everinattentive 
global outregopt landscape plain coljust(lc) varlabels ///
  stats(b se p) nostar sdec(3) // starloc(1) starlevels(10 5 1)
global outregfinal landscape plain coljust(lc) varlabels ///
  stats(b se p) nostar 
global expvar ib0.tech c.comm 1.commabovecurrent ///
  0.opt 1.displayedsecond   
  
outreg, clear(regskill) 
 
cmrologit pref $expvar if $sample & selfseghigh == 0 , cl(id)
  local subj : display %6.0f  e(N_clust)
  local ll : display %9.1f e(ll) 
 domarginswithcurrentcomm if opt!=0 & e(sample) , dydx(1.tech) expression( exp(xb()) / ( 1 + exp(xb()) ) * 100 )
  local matrix A0 = r(table)
  matrix list A0
  local ls0 : display %5.2f A0[1,2] 
  local ls0p : display %5.3f A0[4,2] 
  local ls0lc : display %5.2f A0[5,2]
  local ls0uc : display %5.2f A0[6,2]
 
 nlcom cwd : -_b[1.tech] / _b[c.comm]
  matrix A = r(table)
  local cwd : display %5.2f A[1,1] 
  local cwdp : display %5.3f A[4,1]
  local cwdlc : display %5.2f A[5,1]
  local cwduc : display %5.2f A[6,1]
 outreg , $outregopt ///
  ctitle("VARIABLES" , "Driving knowledge low") ///
  addrows("Subjects", "`subj'" \ "LogL", "`ll'" \ ///
   "CWD", "`cwd'" \ "", "[`cwdp']" \ ///
   "Work", "`ls0'" \ "", "[`ls0p']" ) ///
  merge(regskill)  

cmrologit pref $expvar if $sample & selfseghigh == 1 , cl(id)
  local subj : display %6.0f  e(N_clust)
  local ll: display %9.1f e(ll) 
domarginswithcurrentcomm if opt!=0 & e(sample) , dydx(1.tech) expression( exp(xb()) / ( 1 + exp(xb()) ) * 100 )
  local matrix A0 = r(table)
  matrix list A0
  local ls0 : display %5.2f A0[1,2] 
  local ls0p : display %5.3f A0[4,2] 
  local lslc0 : display %5.2f A0[5,2]
  local lsuc0 : display %5.2f A0[6,2]

 nlcom cwd : -_b[1.tech] / _b[c.comm]
  matrix A = r(table)
  local cwd : display %5.2f A[1,1] 
  local cwdp : display %5.3f A[4,1]
  local cwdlc : display %5.2f A[5,1]
  local cwduc : display %5.2f A[6,1]
 
 outreg , $outregopt ///
  ctitle("VARIABLES" , "Driving knowledge high") ///
  addrows("Subjects", "`subj'" \ "LogL", "`ll'" \ ///
   "CWD", "`cwd'" \ "", "[`cwdp']"  \ ///
   "Work", "`ls0'" \ "", "[`ls0p']" ) ///
  merge(regskill)  

file open regs2 using "reg_skill2_map_all.txt" , ///
   write replace   // compile data for Figure 2 
file write regs2 "selfseghigh" _tab "cwd" _tab "ls" ///
   _tab "lc" _tab "uc" _newline
   
cmrologit pref $expvar ib0.selfseghigh#1.tech ///
   ib0.selfseghigh#c.comm if $sample , cl(id)
  local subj : display %6.0f  e(N_clust)
  local ll: display %9.1f e(ll)    
domarginswithcurrentcomm if opt!=0 & e(sample) , dydx(1.tech) expression( exp(xb()) / ( 1 + exp(xb()) ) * 100 ) at1(selfseghigh=0) at2(selfseghigh=1) statname(ls) fmt(%9.1f)
  local matrix A0 = r(table)
  matrix list A0
  local ls0 : display %5.2f A0[1,3] 
  local ls0p : display %5.3f A0[4,3]
  local ls0lc : display %5.2f A0[5,3]
  local ls0uc : display %5.2f A0[6,3]
  local ls1 : display %5.2f A0[1,4] 
  local ls1p : display %5.3f A0[4,4]
  local ls1lc : display %5.2f A0[5,4]
  local ls1uc : display %5.2f A0[6,4]
  file write regs2 "0" _tab "" _tab (`ls0') _tab (`ls0lc') ///
    _tab (`ls0uc') _newline 
  file write regs2 "1" _tab "" _tab (`ls1') _tab (`ls1lc') ///
    _tab (`ls1uc') _newline 
	
 nlcom cwd0 : -_b[1.tech] / _b[c.comm]
  matrix A0 = r(table)
  local cwd0 : display %5.2f A0[1,1] 
  local cwd0p : display %5.3f A0[4,1]
  local cwd0lc : display %5.2f A0[5,1]
  local cwd0uc : display %5.2f A0[6,1]
  file write regs2 "0" _tab (`cwd0') _tab "" _tab (`cwd0lc') ///
    _tab (`cwd0uc') _newline  
	
 nlcom cwd1 : - ( _b[1.tech] + _b[1.selfseghigh#1.tech] ) / ///
   ( _b[c.comm] + _b[1.selfseghigh#c.comm] )
  matrix A1 = r(table)
  local cwd1 : display %5.2f A1[1,1] 
  local cwd1p : display %5.3f A1[4,1]  
  local cwd1lc : display %5.2f A1[5,1]
  local cwd1uc : display %5.2f A1[6,1]
  file write regs2 "1" _tab (`cwd1') _tab "" _tab (`cwd1lc') ///
   _tab (`cwd1uc') _newline 
  file close regs2
  
outreg , $outregopt ///
  ctitle("VARIABLES" , "Contingent: driving knowledge") ///
  addrows( "Subjects", "`subj'" \ "LogL", "`ll'" \ ///
   "CWD low skill", "`cwd0'" \ "", "[`cwd0p']" \ ///
   "CWD high skill", "`cwd1'" \ "", "[`cwd1p']" \ ///
   "Work low skill", "`ls0'" \ "", "[`ls0p']" \ ///
   "Work high skill", "`ls1'" \ "", "[`ls1p']" ) ///
  merge(regskill)  

outreg using "Table2.doc", ///
  $outregfinal replay(regskill) ///
  note("Estimated by Stata routine, cmrologit; Dependent variable: Choice; ") ///
  replace

****************************************************************** 
*** Figure 2. Vignette experiment: Effect of map app technology **
****************************************************************** 

import delimited "reg_skill2_map_all.txt" , ///
  delimiters(tab) clear

label var cwd "Compensating differential"  
label var ls "Probability of work"

foreach x in cwd ls { 
 local varlabel : variable label `x'
 global graphopt barw(0.5) lw(medium) mlw(medthick) ///
   color("33 102 172") 
 twoway ( bar `x' selfseghigh if selfseghigh == 0 , ///
    $graphopt bargap(-50) ) ///
  ( bar `x' selfseghigh if selfseghigh == 1 , ///
    $graphopt bargap(50) ) ///
  ( rcap lc uc selfseghigh if !mi(`x') , lwidth(thick) ///
    lcolor(gs5) /* color("178 24 43") */ )   ///
  ( scatter `x' selfseghigh if !mi(`x') , msymbol(i) ///
    mlabel(`x') mlabpos(1) mlabsize(large) mlabcol(gs0) ) , /// 
  xtitle( "Driving knowledge" , size(large) ) ///
  xscale( r(-0.5, 1.5) ) ///
  xlabel( 0 "Low" 1 "High" , noticks labsize(large) ) /// 
  ytitle( "`varlabel'" , size(large) ) ///
   ylabel( , format(%3.1f) labsize(large) ) ///
   legend( off ) scheme(burd)
 graph export "Figure2_`x'_skill_all.png" , ///
 replace as(png) 
}


****************************************************
*** Table 3. Field experiment: Summary statistics **
****************************************************

use "field_experiment.dta" , clear
keep hypo work tech id group destination difficulty ///
     male visualage parttime selfknowledge riskaverse ///
     intrinsic fare sampen* ratio* hf* lf* si* sdnn* stai hr* hrv* rmssd*  ///
     predrivingduration drivingduration selfseghigh ///
     riskaverse_skill intrinsic_skill


qui hrv 3_20_0 4_20_20

keep if hypo == 0
replace hr3 = . if work == 0
replace rmssd3 = . if work == 0
gen staihigh = stai > 5 if !mi(stai)

local vars male visualage parttime selfknowledge riskaverse ///
 intrinsic fare work rmssd3 rmssd4 stai

iebaltab `vars', groupvar(tech) total rowvarlabels ///
 stats(desc(sd) pair(p)) vce(robust) ///
 savexlsx("Table3.xlsx") replace

foreach i in rmssd3 rmssd4 stai {
	sum `i', d
	local `i'1 : display %9.3f r(p50)
	sum `i' if tech == 0, d
	local `i'2 : display %9.3f r(p50)
	sum `i' if tech == 1, d
	local `i'3 : display %9.3f r(p50)
}

summarize predrivingduration if work == 1 , d
summarize drivingduration if work == 1 , d

**********************************************************************
*** Table 4. Field experiment: Effect of map app technology on work **
**********************************************************************

use "field_experiment.dta" , clear
qui hrv 3_20_0 4_20_20

global sample hypo == 0 // All destinations: N = 150 
global method reg // probit //logit // 
global regopt vce(cluster id) 
global outregopt landscape plain coljust(lc) varlabels ///
  stats(b se p) nostar // starloc(1) starlevels(10 5 1)
  
outreg, clear(regwork) 
$method work tech if $sample & selfseghigh == 0  , $regopt 
fieldregstat "work" "tech" "(a) Low-skill subjects" ///
  "regwork"

$method work tech if $sample & selfseghigh == 1  , $regopt 
fieldregstat "work" "tech" "(b) High-skill subjects" ///
  "regwork"

$method work tech selfseghigh if $sample , $regopt 
fieldregstat "work" "tech selfseghigh" "(c) All subjects" ///
  "regwork"

$method work i.tech i.selfseghigh 1.tech#1.selfseghigh ///
  if $sample , $regopt
fieldregstatskill "work" "work_skill.txt" ///
  "1.tech 1.selfseghigh 1.tech#1.selfseghigh" ///
  "(d) All subjects: Contingent on skill" "regwork"  

outreg using "Table4.doc" , ///
  $outregopt replay(regwork) ///
  note("Estimated by $method;" ///
   "Dependent variable: Accept work; Robust standard errors clustered by subject in parentheses; p-values in brackets ") ///
  replace  


***********************************************************************
*** Figure 3. Vignette experiment: Effect of technology on work *******
*** Decomposition to amenity and productivity *************************
***********************************************************************

use "vignette_experiment.dta", clear
global sample inrange(pair,11,19) & !everinattentive 
global expvar ib0.tech c.comm 1.commabovecurrent ///
  0.opt 1.displayedsecond  
  
cmrologit pref $expvar ib0.selfseghigh#1.tech ib0.selfseghigh#c.comm ///
  if $sample , cl(id)
replace comm = currentcomm if opt!=0
margins if opt!=0 & e(sample), ///
  dydx(1.tech) expression( exp(xb()) / ( 1 + exp(xb()) ) * 100 ) ///   
  at(selfseghigh = (0 1)) post

nlcom (ls0: _b[1.tech:1._at]) ///
  (p0: 2.88*(_b[1.tech:2._at] ///
  - (_b[1.tech:1._at] - 2.88*_b[1.tech:2._at])/(1.60-2.88))) ///
  (a0: 1.60*(_b[1.tech:1._at] - 2.88*_b[1.tech:2._at])/(1.60-2.88)) ///
  (ls1: _b[1.tech:2._at]) ///
  (p1: _b[1.tech:2._at] ///
  - (_b[1.tech:1._at] - 2.88*_b[1.tech:2._at])/(1.60-2.88)) ///
  (a1: (_b[1.tech:1._at] - 2.88*_b[1.tech:2._at])/(1.60-2.88))

matrix a = r(table)' 
clear
svmat a, names(col)	  

rename b mean
rename ll lc
rename ul uc
gen index = _n
replace index = mod(index,6)
replace index = 6 if index == 0
replace index = index + 1 if index > 3

global ls inlist(index, 1, 5)
global p inlist(index, 2, 6)
global a inlist(index, 3, 7)
format mean %4.2f
format lc* uc* %4.1f

global graphopt barw(0.5) lw(medium) mlw(medthick) 
global color1 color("33 102 172")   // color("178 24 43")
global color2 color("178 24 43")
global color3 color("27 120 55")
global rcapopt lwidth(medthick) lcolor(gs5) 
global estopt msymbol(i) mlabel(mean) mlabpos(1) mlabsize(large) ///
  mlabcol(gs0) 

twoway ( bar mean index if $ls , $color1 ) ///
	( rcap lc uc index if $ls , $rcapopt ) ///
	( scatter mean index if $ls , $estopt ) ///
	( bar mean index if $p , $color3 ) ///
	( rcap lc uc index if $p , $rcapopt ) ///
	( scatter mean index if $p , $estopt ) ///
	( bar mean index if $a , $color2 ) ///
	( rcap lc uc index if $a , $rcapopt ) ///
	( scatter mean index if $a , $estopt ) , ///
	xtitle( "Driving knowledge" , size(large) ) ///
	xlabel( 2 "Low" 6 "High" , noticks labsize(large) ) ///
	yscale( range(-20 30) ) ///
	ytitle( "Effect of tech on prob of work" , size(large) ) ///
	ylabel( , format(%3.1f) labsize(large) ) ///
	legend( order(1 "Prob of work" 4 "Productivity" 7 "Amenity" ) ///
    pos(6) row(1) size(large) ) 
graph export "Figure3.png" , ///
	replace as(png)
	
**************************************************************************
*** Figure 5. Field experiment: Stress ***
**************************************************************************

use "field_experiment.dta" , clear

qui hrv 3_20_0 4_20_20
drop if mi(rmssd4)   // limit to subjects who performed driving task

** RMSSD **
gen extreme = 0
gsort -rmssd_delta2
replace extreme = 0.12 if rmssd_delta2 > 1

global graphopt lw(medthick) 
global method kdensity  
twoway ( $method rmssd_delta2 if tech == 0 , $graphopt lpattern(dash)) ///
  ( $method rmssd_delta2 if tech == 1 , $graphopt ) ///
  (scatter extreme rmssd_delta2 if extreme > 0 , ///
      msymbol(Sh) mcolor(black) msize(medlarge) mlwidth(medthick) ) , ///
  xtitle( "Percent change in RMSSD (driving less pre-drive)" , ///
    size(medlarge) ) xlabel( , labsize(medlarge) ) ///
  ytitle( "Density", size(medlarge) ) ///
  ylabel( , labsize(medlarge) format(%02.1f) ) ///
  legend( pos(6) size(medlarge) ///
    order( 1 "No tech" 2 "With tech") )
graph export "Figure5(a).png", 	replace as(png)

ksmirnov rmssd_delta2 , by(tech) exact
ksmirnov rmssd_delta2 if extreme == 0 , by(tech) exact


** STAI **
ssc install twoway_parea   
local W 0.30 
drop if mi(stai)
contract tech stai
bysort tech: gen total = sum(_freq)
bysort tech (stai): replace total = total[_N]
gen density = _freq / total
expand 2, generate(edge)
gen stai_graph = .
replace stai_graph = stai - `W' + tech * `W'  if edge==0   // left edge
replace stai_graph = stai + tech * `W' if edge==1   // right edge
sort tech stai_graph
drop edge

local i `"parea density stai_graph if tech==0 & stai==5, sort pattern(pattern4) color("33 102 172")|| parea density stai_graph if tech==1 & stai==5, sort color("178 24 43 %50")"'

foreach stai of numlist 6/10 {
    local i `"`i' || parea density stai_graph if tech==0 & stai==`stai', sort pattern(pattern4) color("33 102 172")|| parea density stai_graph if tech==1 & stai==`stai', sort color("178 24 43 %50")"'
}

twoway `i'  , ///
  xtitle( STAI (State-Trait Anxiety Inventory) , size(medlarge) ) ///
  xlabel( , labsize(medlarge) ) ///
  ytitle( Density , size(medlarge) ) ///
  ylabel( , labsize(medlarge) format(%02.1f) ) ///
  legend( pos(6) size(medlarge) ///
    order( 1 "No tech" 2 "With tech") )

graph export "Figure5(b).png" , ///
   replace as(png)

***********************************************************************
*** Figure 6. Field experiment: Effect of map app technology on work **
***********************************************************************

use "field_experiment.dta" , clear
qui hrv 3_20_0 4_20_20
global sample hypo == 0 // All destinations: N = 150 
global method reg // probit //logit // 
global regopt vce(cluster id) 
global outregopt landscape plain coljust(lc) varlabels ///
  stats(b se p) nostar // starloc(1) starlevels(10 5 1)

postutil clear
postfile results str20 group coef lb ub using results, replace
foreach seg in 0 1 {
    reg work i.tech if selfseghigh == `seg' & hypo == 0, robust
    local coef = _b[1.tech]
    local se = _se[1.tech]
    local lb = `coef' - invttail(e(df_r), 0.025) * `se'
    local ub = `coef' + invttail(e(df_r), 0.025) * `se'
    if `seg' == 1 {
        post results ("High self-seg") (`coef') (`lb') (`ub')
    }
    else {
        post results ("Low self-seg") (`coef') (`lb') (`ub')
    }
}
postclose results
preserve
use results, clear
gen selfseghigh = (group == "High self-seg")
global graphopt barw(0.5) lw(medium) mlw(medthick) ///
   color("33 102 172") 
format coef  %04.2f
twoway ( bar coef selfseghigh if selfseghigh == 0 , ///
    $graphopt bargap(-50) ) ///
  ( bar coef selfseghigh if selfseghigh == 1 , ///
    $graphopt bargap(50) ) ///
  ( rcap lb ub selfseghigh if !mi(coef) , lwidth(thick) ///
    lcolor(gs5) /* color("178 24 43") */ )   ///
  ( scatter coef selfseghigh if !mi(coef) , msymbol(i) ///
    mlabel(coef) mlabpos(1) mlabsize(large) mlabcol(gs0) ) , /// 
  xtitle( `"{fontface "Helvetica":Driving knowledge}"' , size(large) ) ///
  xscale( r(-0.5, 1.5) ) ///
  xlabel( 0 `"{fontface "Helvetica":Low}"' 1 `"{fontface "Helvetica":High}"' , noticks labsize(large) ) /// 
  ytitle( `"{fontface "Helvetica":`varlabel'}"' , size(large) ) ///
  ylabel( , format(%3.1f) labsize(large) ) ///
  yscale(r(0.25, .)) ///
   legend( off ) scheme(burd)
  graph export "Figure6.png" , ///
 replace as(png) 
restore 


************************************************
*** Table 5. Field experiment: Heterogeneity ***
************************************************
outreg, clear(regwork) 

$method work i.tech i.selfseghigh 1.tech#1.selfseghigh ///
  if $sample & difficulty == 1, $regopt
fieldregstatskill "work" "work_skill_hetero.txt" ///
  "1.tech 1.selfseghigh 1.tech#1.selfseghigh" ///
  "(a) Easy" "regwork"  

$method work i.tech i.selfseghigh 1.tech#1.selfseghigh ///
  if $sample & difficulty == 2, $regopt
fieldregstatskill "work" "work_skill_hetero.txt" ///
  "1.tech 1.selfseghigh 1.tech#1.selfseghigh" ///
  "(b) Intermediate" "regwork"  

$method work i.tech i.selfseghigh 1.tech#1.selfseghigh ///
  if $sample & difficulty == 3, $regopt
fieldregstatskill "work" "work_skill_hetero.txt" ///
  "1.tech 1.selfseghigh 1.tech#1.selfseghigh" ///
  "(c) Hard" "regwork"  

$method work i.tech i.selfseghigh 1.tech#1.selfseghigh ///
  if $sample & difficulty != 3, $regopt
fieldregstatskill "work" "work_skill_hetero.txt" ///
  "1.tech 1.selfseghigh 1.tech#1.selfseghigh" ///
  "(d) Easy and intermediate" "regwork"  

outreg using "Table5.doc" , ///
  $outregopt replay(regwork) ///
  note("Estimated by $method;" ///
   "Dependent variable: Accept work; Robust standard errors clustered by subject in parentheses; p-values in brackets ") ///
  replace  

***********************************************
****** Erase Intermediate File generated ******
***********************************************
erase "work_skill.txt"
erase "work_skill_hetero.txt"
erase "reg_skill2_map_all.txt"
erase "results.dta"
   